#include "atom.h"
#include "epaper.h"
#include "e-Paper/EPD_2IN13_V2.h"
#include "GUI/GUI_Paint.h"
#include "imagedata.h"
#include "rtc.h"
#include "eventmgr.h"
#include "debug.h"
#include "numberical.h"
#include "lunar/lunar.h"

static const uint8_t *_imageList[] = {
    gImage_yifeng,
    gImage_dog,
    gImage_girl
};

void rtc_wakeup_callback(RtcWakeUp *wakeup)
{
    event_mgr_set(EVENT_RTC_WAKEUP, 0);
}

static UBYTE BlackImage[2756] = {0};
static RtcWakeUp _wakeup = { 0, RTCCLK_Div16, 0, rtc_wakeup_callback };
static int _power_save = 0;

static void rtc_wakeup(int onoff, int sec)
{
    _wakeup.enable = onoff;
    _wakeup.counter = 2048 * sec;
    RtcSetWakeUp(&_wakeup);
}

void epaper_init(int sec)
{
    KPrint("e-Paper Init and Clear...\r\n");
 
    DEV_Module_Init();
    EPD_2IN13_V2_Init(EPD_2IN13_V2_FULL);
    EPD_2IN13_V2_Clear();
    
    atomTimerDelay(20);

    KPrint("Paint_NewImage\r\n");
    Paint_NewImage(BlackImage, EPD_2IN13_V2_WIDTH, EPD_2IN13_V2_HEIGHT, 270, WHITE);
    Paint_SelectImage(BlackImage);
    Paint_Clear(WHITE);

    KPrint("Drawing logo\r\n");
    EPD_2IN13_V2_Display((UBYTE*)gImage_yifeng);
    atomTimerDelay(200);

    KPrint("Drawing image\r\n");
    Paint_DrawBitMap((UBYTE*)gImage_girl);
    EPD_DrawFonts (140, 40, 1, "sync", &Ascii_YouYuan_8x16, &utf8_CN_16x16, 0);
    
    EPD_2IN13_V2_Display(BlackImage);
      
    KPrint("Goto Sleep...\r\n");
    EPD_2IN13_V2_Sleep();
    
    KPrint("close 5V, Module enters 0 power consumption ...\r\n");
   
    if( !_wakeup.enable ){
        //启动RTCwakeup唤醒
        rtc_wakeup(1, sec);
    }
}

static const char WEEKCN[7][4] =
{ "日", "一", "二", "三", "四", "五", "六", };


static int Calculate_Week( int year , int month, int day )
{
	int c,y,week;
	if (month == 1 || month == 2)
	year--, month += 12;
	c = year / 100;
	y = year - c * 100;
	week = y + y / 4 + c / 4 - 2 * c + 26 * (month + 1) / 10 + day - 1;
	while (week < 0)
	week += 7;
	week %= 7;
	return week;
}

void epaper_showRtc(int full)
{
    char buf[64] = {0};
    struct Lunar_Date Lunar;
    
    //刷新画面情况下解除powersave,强制进行一次全局刷新
    if( _power_save == 1){
        _power_save = 0;
        full = 1;
    }
    
    if(full){
        EPD_2IN13_V2_Init(EPD_2IN13_V2_FULL);
        Paint_Clear(WHITE);
    }else{
        EPD_2IN13_V2_Init(EPD_2IN13_V2_PART);
        EPD_2IN13_V2_DisplayPartBaseImage(BlackImage);
    }
    
    int year, month, day, hour, minute, second;
    RtcGetDatetime(&year, &month, &day, &hour, &minute, &second);
    
    LUNAR_SolarToLunar (&Lunar, (uint16_t) year, month, day);
//    l_vsnprintf (buf, 64, "%04d年%02d月%02d日 星期%s %s%s%s", (uint32_t)year, (uint32_t)month, (uint32_t)day, WEEKCN[Calculate_Week(year, month, day)],
//             Lunar_MonthLeapString[Lunar.IsLeap], Lunar_MonthString[Lunar.Month], Lunar_DateString[Lunar.Date]);
    l_vsnprintf (buf, 64, "%04d年%02d月%02d日 星期%s", (uint32_t)year, (uint32_t)month, (uint32_t)day, WEEKCN[Calculate_Week(year, month, day)]);
    KPrint(buf);
    EPD_DrawFonts (8, 1, 1, buf, &Ascii_YouYuan_8x16, &utf8_CN_16x16, 0);

    uint8_t rday = 0;
    uint8_t JQday = GetJieQiStr (year, month, day, &rday);
    KPrintf (" 离%s还有%d天\r\n", JieQiStr[JQday], (uint32_t)rday);

    l_vsnprintf (buf, 64, "%s%s【%s】年", Lunar_StemStrig[LUNAR_GetStem (&Lunar)],
             Lunar_BranchStrig[LUNAR_GetBranch (&Lunar)], Lunar_ZodiacString[LUNAR_GetZodiac (&Lunar)]);
    KPrint(buf);
    
    //EPD_DrawFonts (8, 16, 1, buf, &Ascii_YouYuan_8x16, &utf8_CN_16x16, 0);
    
    EPD_DrawFonts (84, 28, 0, ":", &Ascii_DigitalDismay_28x56, NULL, 0);
    l_vsnprintf (buf, 64, "%02d", (uint32_t)hour);
    EPD_DrawFonts (3, 28, 1, buf, &Ascii_DigitalDismay_28x56, NULL, 0);
    l_vsnprintf (buf, 64, "%02d", (uint32_t)minute);
    EPD_DrawFonts (113, 28, 1, buf, &Ascii_DigitalDismay_28x56, NULL, 0);
 
    EPD_2IN13_V2_Display((UBYTE*)BlackImage);
    atomTimerDelay(10);//Analog clock 1s
 
    KPrint("Goto Sleep...\r\n");
    EPD_2IN13_V2_Sleep();
    KPrint("close 5V, Module enters 0 power consumption ...\r\n");
}



void epaper_powerSave()
{
  if( !_power_save ){
    EPD_2IN13_V2_Init(EPD_2IN13_V2_FULL);
    EPD_2IN13_V2_Clear();
    KPrint("Goto Sleep...\r\n");
    EPD_2IN13_V2_Sleep();
    atomTimerDelay(10);//Analog clock 1s
    _power_save = 1;
  }
}

void epaper_showPic(int index)
{
    int len = sizeof(_imageList) / sizeof(&_imageList[0]);

    if(index >= len)
        return;
    
    //刷新画面情况下解除powersave
    _power_save = 0;

    EPD_2IN13_V2_Init(EPD_2IN13_V2_FULL);
    EPD_2IN13_V2_Clear();
    
    atomTimerDelay(20);

    KPrint("Paint_NewImage\r\n");
    Paint_NewImage(BlackImage, EPD_2IN13_V2_WIDTH, EPD_2IN13_V2_HEIGHT, 270, WHITE);
    Paint_SelectImage(BlackImage);
    Paint_Clear(WHITE);

    KPrint("Drawing logo\r\n");
    EPD_2IN13_V2_Display((UBYTE*)_imageList[index]);
    atomTimerDelay(200);

    KPrint("Goto Sleep...\r\n");
    EPD_2IN13_V2_Sleep();
    atomTimerDelay(10);//Analog clock 1s

    KPrint("close 5V, Module enters 0 power consumption ...\r\n");
}